Base.namespace('Base');

/**
 * Class: Base.SelectControl
 * @class 
 */
Base.SelectControlStatic = {
    
    map              : null,
    layers           : null,
    selectControl    : null,
    highlightControl : null,

    tooltipOn        : true,
    tooltipWidth     : 150,
    tooltipHeight    : 16,
    tooltipPopup     : null,

    featurePopup     : null,

    /**
     */
    initialize : function(config)
    {
        Base.configure(this, config);

        // only highlight
        this.highlightControl = new OpenLayers.Control.SelectFeature(
            this.layers,
            {
                clickout:      true, 
                hover:         true,
                renderIntent:  "temporary",
                highlightOnly: true
            }
        );

        //var THIS = this; 
        this.selectControl = new OpenLayers.Control.SelectFeature(
            this.layers,
            {
                clickout:       true, 
                toggle:         true,
                hover:          false,
                onSelect:       this.featurePopupOn,
                onUnselect:     this.featurePopupOff,
                onBeforeSelect: this.beforeSelect,
                scope:          this,      // for onSelect and onUnselect methods (see feature callbacks below)
                toggleKey:      "ctrlKey", // ctrl key removes from selection
                multipleKey:    "shiftKey" // shift key adds to selection
            }
        );

        // when multiple features selected, we need to do special processing...
        this.parentSelectBox = this.selectControl.selectBox;
        // TODO some type of handler magic here for multiple selects
        // TODO bbox select should have both a select button and a ctl-key on option

        // IMPORTANT NOTE: the order of highlight, then select is important
        this.map.addControl(this.highlightControl);
        this.map.addControl(this.selectControl);

        this.highlightControl.activate();
        this.selectControl.activate();

        this.newTooltipPopup();
        this.newFeaturePopup();
    },

    /** position    {OpenLayers.Bounds || OpenLayers.Pixel } */
    selectBox : function(position)
    {
        var x = 1;
        this.parentSelectBox(position);
        ;
    },

    /** */
    newTooltipPopup : function()
    {
        this.tooltipPopup = new OpenLayers.Popup("tooltip", 
                null,
                new OpenLayers.Size(this.tooltipWidth, this.tooltipHeight),
                null,
                false);

        this.tooltipPopup.setBackgroundColor('white');
        this.tooltipPopup.setOpacity(0.90);
        this.map.addPopup(this.tooltipPopup);
        this.tooltipPopup.hide();

        this.highlightControl.events.register("featurehighlighted",   this,  this.tooltipPopupOn);
        this.highlightControl.events.register("featureunhighlighted", this,  this.tooltipPopupOff);
        this.highlightControl.events.register("featuresremoved",      this,  this.tooltipPopupOff);
    },

    /** */
    newFeaturePopup : function()
    {
        this.featurePopup = new OpenLayers.Popup("feature", 
                null,
                new OpenLayers.Size(this.tooltipWidth, this.tooltipHeight),
                null,
                false       );

        this.featurePopup.setBackgroundColor('blue');
        this.featurePopup.setOpacity(0.90);
        this.map.addPopup(this.featurePopup);
        this.featurePopup.hide();
    },

    
    /** */
    tooltipPopupOn : function(event)
    {
        try
        {
            // controls whether a tooltip is shown
            if(this.tooltipOn === false) return;

            var feature = event.feature;
            if(feature == null || feature.attributes.id == null || feature.attributes.type == null) 
                return;

            // show stop id                
            var ll = feature.geometry.getBounds().getCenterLonLat();
            this.tooltipPopup.lonlat = new OpenLayers.LonLat(ll.lon, ll.lat); 
            this.tooltipPopup.updatePosition();
            this.tooltipPopup.setContentHTML("&nbsp;&nbsp;&nbsp;" + "  Line ");
            this.tooltipPopup.show();
        }
        catch(e)
        {
        }
    },

    /** */
    tooltipPopupOff : function()
    {
        if(this.tooltipPopup)
            this.tooltipPopup.hide();
    },

    /** */
    beforeSelect : function(feature)
    {
        this.tooltipPopupOff();
        if(this.clickOffCB) 
            this.clickOffCB();
    },

    /** */
    featurePopupOn : function(feature)
    {
        try
        {
            if(feature == null || feature.attributes.id == null || feature.attributes.type == null) 
                return;

            // show stop id
            var ll = feature.geometry.getBounds().getCenterLonLat();
            this.featurePopup.lonlat = new OpenLayers.LonLat(ll.lon, ll.lat); 
            this.featurePopup.updatePosition();
            this.featurePopup.setContentHTML("&nbsp;&nbsp;&nbsp;" + " Feature ");
            this.featurePopup.show();
        }
        catch(e)
        {
        }
    },

    featurePopupOff : function()
    {
        if(this.featurePopup)
            this.featurePopup.hide();
    },
    
    
    /** */
    setLayer : function(layers)
    {
        if(layers == null) return;
        if(layers.length == null || layers.length < 2) return;

        //
        // IMPORTANT NOTE: the order of highlight, then select is important
        //
        if(this.highlightControl) this.highlightControl.setLayer(layers);
        if(this.selectControl)    this.selectControl.setLayer(layers);
    },

    CLASS_NAME: "Base.SelectControl"
}

try
{
    Base.SelectControl = new Base.Class(Base.SelectControlStatic);
}
catch(e)
{
    console.log("Base.SelectControl: error creating this type...please ignore this error if you are not using OpenLayers");
}
